home *** CD-ROM | disk | FTP | other *** search
/ X User Tools / X User Tools (O'Reilly and Associates)(1994).ISO / sources / xmenu / part01.z / part01
Text File  |  1994-09-27  |  10KB  |  344 lines

  1. Path: uunet!decwrl!sun-barr!newstop!sun!ukc.ac.uk
  2. From: rlh2@ukc.ac.uk (Richard Hesketh)
  3. Newsgroups: comp.sources.x
  4. Subject: v08i008:  xmenu - popup menus from shell scripts, Part01/01
  5. Message-ID: <137922@sun.Eng.Sun.COM>
  6. Date: 27 Jun 90 06:51:38 GMT
  7. Sender: news@sun.Eng.Sun.COM
  8. Lines: 333
  9. Approved: argv@sun.com
  10.  
  11. Submitted-by: Richard Hesketh <rlh2@ukc.ac.uk>
  12. Posting-number: Volume 8, Issue 8
  13. Archive-name: xmenu/part01
  14.  
  15. Someone asked for this facility on comp.windows.x and as I could with one
  16. as well .. here is one.  It is also an example use of the Athena
  17. Simple Menu widget.
  18.  
  19. Richard Hesketh   :   @nsfnet-relay.ac.uk:rlh2@ukc.ac.uk
  20.           :   rlh2@ukc.ac.uk    ..!mcvax!ukc!rlh2
  21. ---                                               
  22. Computing Lab., University of Kent at Canterbury,
  23. Canterbury, Kent, CT2 7NF, United Kingdom.    Tel: +44 227 764000 ext 3682/7620
  24.  
  25. #! /bin/sh
  26. # This is a shell archive, meaning:
  27. # 1. Remove everything above the #! /bin/sh line.
  28. # 2. Save the resulting text in a file.
  29. # 3. Execute the file with /bin/sh (not csh) to create the files:
  30. #    README
  31. #    Imakefile
  32. #    patchlevel.h
  33. #    xmenu.man
  34. #    xmenu.c
  35. # This archive created: Tue Jun 19 17:02:52 1990
  36. # By:    Richard Hesketh ()
  37. export PATH; PATH=/bin:$PATH
  38. if test -f 'README'
  39. then
  40.     echo shar: will not over-write existing file "'README'"
  41. else
  42. cat << \SHAR_EOF > 'README'
  43. XMenu - provide a popup menu from a shell script etc.
  44.  
  45. This little toolkit program produces a popup menu on the screen taking
  46. the command line arguments as menu entries.  If an argument contains
  47. a non-escaped '=' (equals sign) the string to the left is the name
  48. displayed in the menu and the string to the right is the text output
  49. when this menu button is pressed.
  50.  
  51. A special argument "line" can be used to draw a dividing line between
  52. successive menu entries.
  53.  
  54. A menu title can be given as an argument after a "-heading" flag.
  55.  
  56. The menu is only popped down when a menu button is pressed.
  57. By default the menu is popped up below and to the right of the current
  58. pointer position, use "-geometry" to position the menu at a particular
  59. point (and in a particular size). 
  60.  
  61. Examples:
  62.  
  63. xmenu -heading "Choose Files by Suffix" Compressed="*.Z" \
  64.         "C sources"="*.c" "Headers"="*.h" \
  65.         line line "        Cancel"
  66.  
  67. xmenu Hello="Hello World" line " " " " line Goodbye="Goodbye World"
  68.  
  69.  
  70. Enjoy,
  71.  
  72. Richard Hesketh   :   @nsfnet-relay.ac.uk:rlh2@ukc.ac.uk
  73.           :   rlh2@ukc.ac.uk    ..!mcsun!ukc!rlh2
  74. ---                                               
  75. Computing Lab., University of Kent at Canterbury,
  76. Canterbury, Kent, CT2 7NF, United Kingdom.    Tel: +44 227 764000 ext 3682/7620
  77. SHAR_EOF
  78. fi # end of overwriting check
  79. if test -f 'Imakefile'
  80. then
  81.     echo shar: will not over-write existing file "'Imakefile'"
  82. else
  83. cat << \SHAR_EOF > 'Imakefile'
  84.        INCLUDES = -I$(TOP)
  85.         DEPLIBS = XawClientDepLibs
  86. LOCAL_LIBRARIES = XawClientLibs
  87.            SRCS = xmenu.c
  88.            OBJS = xmenu.o
  89.  
  90. SimpleProgramTarget(xmenu)
  91. SHAR_EOF
  92. fi # end of overwriting check
  93. if test -f 'patchlevel.h'
  94. then
  95.     echo shar: will not over-write existing file "'patchlevel.h'"
  96. else
  97. cat << \SHAR_EOF > 'patchlevel.h'
  98. #define patchlevel 0
  99. SHAR_EOF
  100. fi # end of overwriting check
  101. if test -f 'xmenu.man'
  102. then
  103.     echo shar: will not over-write existing file "'xmenu.man'"
  104. else
  105. cat << \SHAR_EOF > 'xmenu.man'
  106. .TH XMENU l "19 June 1990"
  107. .SH NAME
  108. xmenu \- provide simple popup menus to shell scripts
  109. .SH SYNOPSIS
  110. \fBxmenu\fP [-heading "menu title"] [menu_entry[=output_value] | line]...
  111. .SH DESCRIPTION
  112. \fIxmenu\fP provides a quick and easy way of producing a single popup
  113. menu for use by shell scripts.  The \fI-heading\fP is used to set the menu's
  114. title, all other arguments are used as items in the menu.  If an argument
  115. contains an equals sign then the string to the left of the equals is used as
  116. the displayed menu item and the the string to the right is used as the text
  117. output when the item is pressed.  The argument "line" is recognised as
  118. denoting a line separating menu items.
  119.  
  120. By default the menu is popped up to the right and below of the current pointer
  121. position but this can be overridden using the standard toolkit "-geometry". 
  122. .SH EXAMPLES
  123. .DS
  124. xmenu -heading "Choose Files by Suffix" Compressed="*.Z" \\
  125.                        "C sources"="*.c" "Headers"="*.h" \\
  126.                        line line "        Cancel"
  127.  
  128. xmenu Hello="Hello World" line " " " " line Goodbye="Goodbye World"
  129. .DE
  130. .SH AUTHOR
  131. Richard Hesketh, University of Kent at Canterbury, June 1990
  132. .br
  133. @nsfnet-relay.ac.uk:rlh2@ukc.ac.uk
  134. SHAR_EOF
  135. fi # end of overwriting check
  136. if test -f 'xmenu.c'
  137. then
  138.     echo shar: will not over-write existing file "'xmenu.c'"
  139. else
  140. cat << \SHAR_EOF > 'xmenu.c'
  141. #ifndef lint
  142. static char rcsid[] = "$Header: xmenu.c,v 1.1 90/06/19 16:59:36 rlh2 Rel $";
  143. #endif !lint
  144.  
  145. /* 
  146.  * Copyright 1990 Richard Hesketh / rlh2@ukc.ac.uk
  147.  *                Computing Lab. University of Kent at Canterbury, UK
  148.  *
  149.  * Permission to use, copy, modify and distribute this software and its
  150.  * documentation for any purpose is hereby granted without fee, provided that
  151.  * the above copyright notice appear in all copies and that both that
  152.  * copyright notice and this permission notice appear in supporting
  153.  * documentation, and that the names of Richard Hesketh and The University of
  154.  * Kent at Canterbury not be used in advertising or publicity pertaining to
  155.  * distribution of the software without specific, written prior permission.
  156.  * Richard Hesketh and The University of Kent at Canterbury make no
  157.  * representations about the suitability of this software for any purpose.
  158.  * It is provided "as is" without express or implied warranty.
  159.  *
  160.  * Richard Hesketh AND THE UNIVERSITY OF KENT AT CANTERBURY DISCLAIMS ALL
  161.  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
  162.  * OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL Richard Hesketh OR THE
  163.  * UNIVERSITY OF KENT AT CANTERBURY BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  164.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  165.  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  166.  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  167.  * OF THIS SOFTWARE.
  168.  *
  169.  * Author:  Richard Hesketh / rlh2@ukc.ac.uk, 
  170.  *                Computing Lab. University of Kent at Canterbury, UK
  171.  */
  172.  
  173. /* XMenu - provide a popup menu from a shell script etc.
  174.  *
  175.  * This little toolkit program produces a popup menu on the screen taking
  176.  * the command line arguments as menu entries.  If an argument contains
  177.  * a non-escaped '=' (equals sign) the string to the left is the name
  178.  * displayed in the menu and the string to the right is the text output
  179.  * when this menu button is pressed.
  180.  *
  181.  * A special argument "line" can be used to draw a dividing line between
  182.  * successive menu entries.
  183.  *
  184.  * A menu title can be given as an argument after a "-heading" flag.
  185.  *
  186.  * The menu is only popped down when a menu button is pressed.
  187.  * By default the menu is popped up below and to the right of the current
  188.  * pointer position, use "-geometry" to position the menu at a particular
  189.  * point (and in a particular size). 
  190.  *
  191.  * Examples:
  192.  *
  193.  *    xmenu -heading "Choose Files by Suffix" Compressed="*.Z" \
  194.  *            "C sources"="*.c" "Headers"="*.h" \
  195.  *            line line "        Cancel"
  196.  *
  197.  *    xmenu Hello="Hello World" line " " " " line Goodbye="Goodbye World"
  198.  */
  199.  
  200. #include <X11/Intrinsic.h>
  201. #include <X11/StringDefs.h>
  202. #include <X11/Xaw/SimpleMenu.h>
  203. #include <X11/Xaw/SmeBSB.h>
  204. #include <X11/Xaw/SmeLine.h>
  205.  
  206. #define LINE (String)1
  207. #define LINE_TOK "line"
  208.  
  209. static String MTrans = "<EnterWindow>: highlight()\n\
  210.             <LeaveWindow>: unhighlight()\n\
  211.             <MotionNotify>: highlight()\n\
  212.             <BtnUp>: notify() unhighlight()";
  213. static XtTranslations MPTrans = NULL;
  214.  
  215. static XrmOptionDescRec options[] = {
  216.     {"-heading", ".heading", XrmoptionSepArg, (XtPointer)NULL},
  217. };
  218.  
  219.  
  220. static void
  221. button_pressed(w, client_data, call_data)
  222. Widget w;
  223. XtPointer client_data, call_data;
  224. {
  225.     if (client_data != NULL)
  226.         printf("%s\n", client_data);
  227.     exit(0);
  228. }
  229.  
  230. String
  231. get_prompt(str, out_val)
  232. String str;
  233. String *out_val;
  234. {
  235.     int s, t;
  236.     char name[1000];
  237.     Boolean found_escape = FALSE;
  238.  
  239.     *out_val = NULL;
  240.  
  241.     if (strncmp(str, LINE_TOK, strlen(LINE_TOK)) == 0)
  242.         return (LINE);
  243.  
  244.     for (t = s = 0; str[s] != '\0'; s++) {
  245.         if (str[s] == '\\') {
  246.             if (found_escape) {
  247.                 found_escape = FALSE;
  248.                 name[t++] = '\\';
  249.             } else
  250.                 found_escape = TRUE;
  251.         } else if (str[s] == '=') {
  252.             if (found_escape) {
  253.                 found_escape = FALSE;
  254.                 name[t++] = '=';
  255.             } else {
  256.                 *out_val = str + s + 1;
  257.                 break;
  258.             }
  259.         } else
  260.             name[t++] = str[s];
  261.     }
  262.     name[t] = '\0';
  263.     return (XtNewString(name));
  264. }
  265.  
  266.  
  267. main(argc, argv)
  268. int argc;
  269. char *argv[];
  270. {
  271.     Widget shell, button;
  272.     Cardinal i;
  273.     Position x, y;
  274.     int int_x, int_y;
  275.     Window junk1;
  276.     int junk2;
  277.     unsigned int junk3;
  278.     Display *dpy;
  279.     String out_val;
  280.     String name, heading;
  281.     char *blank;
  282.     XrmValue value;
  283.  
  284.     x = y = 400; /* just in case XQueryPointer() fails 8-) */
  285.  
  286.     XtToolkitInitialize();
  287.     dpy = XtOpenDisplay(XtCreateApplicationContext(), NULL,
  288.                     "xmenu", "XMenu",
  289.                     options, XtNumber(options),
  290.                     &argc, argv);
  291.  
  292.     if (XQueryPointer(dpy, DefaultRootWindow(dpy), &junk1, &junk1,
  293.         &int_x, &int_y, &junk2, &junk2, &junk3)) {
  294.         x = int_x;
  295.         y = int_y;
  296.     }
  297.  
  298.     if (XrmGetResource(dpy->db, "xmenu.heading", "", &blank, &value))
  299.         heading = (char *)value.addr;
  300.     else
  301.         heading = NULL;
  302.  
  303.     MPTrans = XtParseTranslationTable(MTrans);
  304.     shell = XtVaAppCreateShell("xmenu", "XMenu", simpleMenuWidgetClass,
  305.                     dpy, XtNx, x, XtNy, y,
  306.                     XtNlabel, heading,
  307.                     XtNtranslations, MPTrans,
  308.                     NULL);
  309.     if (heading != NULL)
  310.         button = XtVaCreateManagedWidget("menuLine", 
  311.                     smeLineObjectClass, shell,
  312.                     XtNlineWidth, 3,
  313.                     NULL);
  314.  
  315.     for (i = 1; i < argc; i++) {
  316.         name = get_prompt(argv[i], &out_val);
  317.  
  318.         if (name == LINE)
  319.             button = XtVaCreateManagedWidget("menuLine",
  320.                     smeLineObjectClass, shell,
  321.                     NULL);
  322.         else {
  323.             button = XtVaCreateManagedWidget("menuButton",
  324.                     smeBSBObjectClass, shell,
  325.                     XtNlabel, name,
  326.                     NULL);
  327.             XtAddCallback(button, XtNcallback, button_pressed,
  328.                     (XtPointer)out_val);
  329.         }
  330.     }
  331.  
  332.     XtRealizeWidget(shell);
  333.     XtAppMainLoop(XtWidgetToApplicationContext(shell));
  334. }
  335. SHAR_EOF
  336. fi # end of overwriting check
  337. #    End of shell archive
  338. exit 0
  339.  
  340. dan
  341. ----------------------------------------------------
  342. O'Reilly && Associates   argv@sun.com / argv@ora.com
  343. Opinions expressed reflect those of the author only.
  344.